package com.zzptc.ly.a11_10_1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class MainActivity extends AppCompatActivity {
DanmakuView danmuView;
LinearLayout lin;
EditText editText;
DanmakuContext danmakuContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        event();
    }
    //创建弹幕解析器
    private BaseDanmakuParser parser=new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };

    private void event() {
        danmuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lin.getVisibility()==View.GONE){
                    lin.setVisibility(View.VISIBLE);
                }else{
                    lin.setVisibility(View.GONE);
                }
            }
        });
        danmuView.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                danmuView.start();//准备发送弹幕
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
        danmuView.enableDanmakuDrawingCache(true);
        danmuView.prepare(parser,danmakuContext);
    }

    private void init() {
        danmuView=findViewById(R.id.danmu);
        lin=findViewById(R.id.linenew);
        editText=findViewById(R.id.et_send);
        danmakuContext=DanmakuContext.create();
    }

    public void send(View view) {
        String str=editText.getText().toString().trim();
        if (str.equals("")){
        }else{
            sendDanmu(str);
        }
    }
//6666666
    private void sendDanmu(String s) {
        BaseDanmaku baseDanmaku=danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        baseDanmaku.text=s;
        baseDanmaku.textSize=40;
        baseDanmaku.textColor= Color.RED;
        baseDanmaku.borderColor=Color.BLUE;
        baseDanmaku.setTime(danmuView.getCurrentTime());
        danmuView.addDanmaku(baseDanmaku);
    }
}
